﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PSTUTemplate.Models;
using PSTUTemplate.Models.Object.ViewModels;
using PSTUTemplate.Models.DBObject;

namespace PSTUTemplate.Controllers
{
    public class AdminController : Controller
    {
        Repository rep = new Repository();
        //
        // GET: /Admin/

        //[Authorize(Roles = "Administrator")]
        public ActionResult Index()
        {
            AdminProfileViewModel profileViewModel = new AdminProfileViewModel();

            profileViewModel.ProfileList = rep.GetProfileList().ToList();
            profileViewModel.StudentProfile = new Profile();
            return View(profileViewModel);
        }
        [HttpPost]
        public ActionResult Index(AdminProfileViewModel profileViewModel)
        {
            List<Profile> studentList = new List<Profile>();
            studentList = rep.GetProfileList().ToList();
            if (string.IsNullOrEmpty(profileViewModel.StudentProfile.Roll))
            {
                profileViewModel.ProfileList = studentList;
            }
            else
            {
                profileViewModel.ProfileList = studentList.FindAll(x => x.Roll == profileViewModel.StudentProfile.Roll);
            }
            return View(profileViewModel);
        }
        //
        // GET: /Admin/Details/5

        public ActionResult Details(int id)
        {
            Profile aProfile = rep.GetAProfileByUserId(id);
            return View(aProfile);
        }

        //
        // GET: /Admin/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Admin/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Admin/Edit/5

        public ActionResult Edit(int id)
        {
            Profile aProfile = rep.GetAProfileByUserId(id);
            return View(aProfile);
        }

        //
        // POST: /Admin/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, Profile aProfile)
        {
            try
            {
                // TODO: Add update logic here
                Profile updateProfile = rep.GetAProfileByUserId(id);

                updateProfile.District = aProfile.District;
                updateProfile.Email = aProfile.Email;
                updateProfile.Faculty = aProfile.Faculty;
                updateProfile.FathersName = aProfile.FathersName;
                updateProfile.HallBorderNo = aProfile.HallBorderNo;
                updateProfile.HallName = aProfile.HallName;
                updateProfile.IsTribial = aProfile.IsTribial;
                updateProfile.Level = aProfile.Level;
                updateProfile.MothersName = aProfile.MothersName;
                updateProfile.Name = aProfile.Name;
                updateProfile.Nationality = aProfile.Nationality;
                updateProfile.Phone = aProfile.Phone;
                updateProfile.PostOffice = aProfile.PostOffice;
                updateProfile.RegistrationNo = aProfile.RegistrationNo;
                updateProfile.Religion = aProfile.Religion;
                updateProfile.Roll = aProfile.Roll;
                updateProfile.RoomNo = aProfile.RoomNo;
                updateProfile.Semester = aProfile.Semester;
                updateProfile.StartSession = aProfile.StartSession;
                updateProfile.Thana = aProfile.Thana;
                updateProfile.VillageRoadArea = aProfile.VillageRoadArea;

                rep.Save();
                return RedirectToAction("Index");
            }
            catch
            {
                return View(aProfile);
            }
        }

        //
        // GET: /Admin/Delete/5

        public ActionResult Delete(int id)
        {
            rep.DeleteAStudentById(id);
            return RedirectToAction("Index");
        }

        //
        // POST: /Admin/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
